package cbs.demo.graal;

import cbs.demo.domain.Patient;
import cbs.demo.service.FooService;
import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.annotate.TargetElement;
import com.oracle.svm.core.jdk.JDK11OrEarlier;
import com.oracle.svm.core.jdk.JDK8OrEarlier;

/**
 * 替换方法的类
 */
@TargetClass(FooService.class)
public final class FooServiceSubstitutions {
    /**
     * 引用原类中的字段
     */
    @Alias
    private Patient patient;

    /**
     * 方法签名需要和原方法一致
     * @param foo
     * @return
     */
    @Substitute
    public String foo(String foo) {
        System.out.println("\nSubstitute改写方法体");
        return "Substitutions:" + (patient == null ? foo : patient.getName());
    }

    @Substitute
    @TargetElement(onlyWith = JDK8OrEarlier.class)
    public String foo8(String foo) {
        System.out.println("\nSubstitute JDK8改写方法体");
        return "Substitutions8:" + (patient == null ? foo : patient.getName());
    }

    @Substitute
    @TargetElement(onlyWith = JDK11OrEarlier.class)
    public String foo11(String foo) {
        System.out.println("\nSubstitute JDK11改写方法体");
        return "Substitutions11:" + (patient == null ? foo : patient.getName());
    }
}
